package com.lovecws.mumu.demo.tio.client;

import com.lovecws.mumu.demo.tio.HelloAbsAioHandler;
import com.lovecws.mumu.demo.tio.HelloPacket;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;

import java.util.Random;

public class HelloClientAioHandler extends HelloAbsAioHandler implements ClientAioHandler<Object, HelloPacket, Object>
{
    /** 
     * 处理消息
     */
    @Override
    public Object handler(HelloPacket packet, ChannelContext<Object, HelloPacket, Object> channelContext) throws Exception
    {
        String userId="babymumu:"+ new Random().nextInt(10);
        byte[] body = packet.getBody();
        if (body != null)
        {
            String str = new String(body, HelloPacket.CHARSET);
            System.out.println("userId:"+userId+" ;收到消息：" + str);

            //HelloClientStarter.send();
        }
        //当前连接绑定到一个用户
        //Aio.bindUser(channelContext,userId);
        //将当前连接 绑定到一个组中
        //Aio.bindGroup(channelContext,"lovecws");
        return null;
    }
 
    private static HelloPacket heartbeatPacket = new HelloPacket();
 
    /** 
     * 此方法如果返回null，框架层面则不会发心跳；如果返回非null，框架层面会定时发本方法返回的消息包
     */
    @Override
    public HelloPacket heartbeatPacket()
    {
        return heartbeatPacket;
    }
}